/**
 * 
 */
package com.jinmei.remote.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * @author richard
 *
 */
public class JSONUtils {

    public static Map<String, Object> parseJSON(String jsonBody) throws JSONException {
        Map<String, Object> params = new HashMap<String, Object>();
        JSONObject obj = new JSONObject(jsonBody);
        Iterator it = obj.keys();
        while (it.hasNext()) {
            Object o = it.next();
            if (o instanceof String) {
                String key = (String)o;
                params.put(key, obj.get(key));
            }
        }
        return params;
    }
    
    public static List<Map<String, Object>> parseJSONArray(String jsonBody) throws JSONException {
    	List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
    	
        JSONArray jsonArray = new JSONArray(jsonBody);
        for (int i = 0; i < jsonArray.length(); i++) {
        	Map<String, Object> params = new HashMap<String, Object>();
        	JSONObject jsonObj = jsonArray.getJSONObject(i);
        	Iterator it = jsonObj.keys();
            while (it.hasNext()) {
                Object o = it.next();
                if (o instanceof String) {
                    String key = (String)o;
                    params.put(key, jsonObj.get(key));
                }
            }
            results.add(params);
        }
        return results;
    }


}
